<template>
  <x-dialog
    title="调整结束时间"
    :close-on-click-modal="false"
    :show.sync="visible"
    v-if="visible"
    lock-scroll
    append-to-body
    width="450px"
    :before-close="handleClose"
    @onCancel="handleClose"
    @onSubmit="handleSubmit"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="rules" label-position="top">
      <el-form-item prop="endDate" label="结束时间">
        <el-date-picker
          v-model="dataForm.endDate"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          default-time="23:59:59"
          placeholder="请选择"
          :picker-options="pickerBeginDateAfter"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>
  </x-dialog>
</template>
<script>
  import { apiPostEnquiryDelay } from '@/modules/purchaseSearchSource/api/enquiry.js'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  const validateDate = (rule, value, callback) => {
    if (new Date(value).getTime() < new Date().getTime() - 8.64e7) {
      callback(new Error('请选择当前日期后'))
    } else {
      callback()
    }
  }
  export default {
    components: {},
    props: {},
    data() {
      return {
        visible: false,
        dataForm: {
          endDate: ''
        }
      }
    },
    computed: {
      pickerBeginDateAfter() {
        return {
          disabledDate: (time) => {
            return new Date(time).getTime() < new Date().getTime() - 86400000
          }
        }
      },
      rules() {
        return {
          endDate: [
            { required: true, message: '请选择', trigger: ['blur'] },
            { validator: validateDate, message: '请选择当前日期后', trigger: ['blur'] }
          ]
        }
      }
    },
    methods: {
      show(enquiryId) {
        this.visible = true
        this.enquiryId = enquiryId
      },
      handleClose() {
        this.$nextTick(() => {
          this.$refs.dataForm.resetFields()
          this.visible = false
        })
      },
      // 弹窗确定
      handleSubmit() {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.handleEnquiryDelay()
          }
        })
      },
      // 取消询价
      async handleEnquiryDelay() {
        const params = {
          enquiryId: this.enquiryId,
          endDate: this.dataForm.endDate
        }
        const { code, msg } = await apiPostEnquiryDelay(params)
        if (code === EnumApiCode.Success) {
          this.$message.success('延期成功')
          this.handleClose()
          this.$emit('on-confirm')
        } else {
          this.$message.error(msg)
        }
      }
    }
  }
</script>
